using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Security.Permissions;
using System.ComponentModel;
using System.Web.UI.Design;
using System.Drawing.Design;

namespace CodeStory.WebControls
{

	[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	[TypeConverter(typeof(ExpandableObjectConverter))]
	public abstract class BaseLinkItem : IStateManager
	{

		StateBag viewState = null;
		bool isTrackingViewState = false;

		[Editor(typeof(UrlEditor), typeof(UITypeEditor))]
		[NotifyParentProperty(true)]
		public string Href {
			get { 
				object obj = ViewState["Href"];
				return obj == null ? string.Empty : (string)obj;	
			}
			set { ViewState["Href"] = value; }
		}

		
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
		protected StateBag ViewState {
			get {
				if (this.viewState == null) {
					this.viewState = new StateBag(false);
					if (this.isTrackingViewState) {
						((IStateManager)this.viewState).TrackViewState();
					}
				}
				return this.viewState;
			}
		}

		internal void SetDirty() {
			if (this.viewState != null) {
				this.viewState.SetDirty(true);
			}
		}

		public virtual string ToHtmlString(Control page) {
			return string.Empty;
		}

		#region IStateManager Members

		[Browsable(false)]
		public bool IsTrackingViewState {
			get {
				return this.isTrackingViewState;
			}
		}

		public void LoadViewState(object state) {
			if (state != null) {
				((IStateManager)this.ViewState).LoadViewState(state);
			}
		}

		protected virtual object SaveViewState() {
			if (this.viewState != null) {
				return ((IStateManager)this.viewState).SaveViewState();
			}
			return null;
		}

		protected virtual void TrackViewState() {
			this.isTrackingViewState = true;
			if (this.viewState != null) {
				((IStateManager)this.viewState).TrackViewState();
			}
		}

		#endregion

		#region IStateManager Members

		bool IStateManager.IsTrackingViewState {
			get { return this.IsTrackingViewState; }
		}

		void IStateManager.LoadViewState(object state) {
			this.LoadViewState(state);
		}

		object IStateManager.SaveViewState() {
			return this.SaveViewState();
		}

		void IStateManager.TrackViewState() {
			this.TrackViewState();
		}

		#endregion
	}

}
